﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls;
using UIT.Controls.Containers;
using UIT.Controls.Input;
using JQueryControlToolkit;
using UIT.Controls.Data;
using System.Xml;

namespace UIT.Web
{
    public class PostBackValueVisitor
    {
        private System.Web.HttpContext context;

        public PostBackValueVisitor(System.Web.HttpContext context)
        {
            // TODO: Complete member initialization
            this.context = context;
        }

        #region IVisitor<Button> Members

        public void Visit(Button visitable)
        {
            visitable.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(visitable_PropertyChanged);
        }

        void visitable_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            IControl boundControl = sender as IControl;
            if (boundControl.GetBinding(e.PropertyName) != null)
                return;
            if (e.PropertyName == "Text")
                ScriptManager.RegisterCall(boundControl.Id + "_txt", new JQuery("#" + boundControl.Id).Val((string)new UIT.Markup.BindingFragment(false) { Member = e.PropertyName }.GetValue(sender)));
        }

        #endregion

        #region IVisitor<Page> Members

        public void Visit(Page visitable)
        {
            Visit((IControlContainer)visitable);
        }

        private void Visit(IControlContainer container)
        {
            foreach (var control in container.Children)
                Visit(control);
        }

        #endregion

        #region IVisitor<TextBox> Members

        public void Visit(TextBox visitable)
        {
            visitable.Text = context.Request.Form[visitable.Id];
            visitable.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(visitable_PropertyChanged);
        }

        #endregion

        public void Visit(DataGrid dataGrid)
        {
            dataGrid.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(dataGrid_PropertyChanged);

        }

        void dataGrid_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Rows")
            {
                StringBuilder sb = new StringBuilder();
                XmlWriter writer = XmlWriter.Create(sb, new XmlWriterSettings { ConformanceLevel = ConformanceLevel.Fragment });
                new WebRendererManager(writer).Manager.Render((IControl)sender);
                writer.Flush();
                ScriptManager.RegisterCall(((IControl)sender).Id + "_rows", new JQuery("#" + ((IControl)sender).Id + " > tbody").Html(new JQuery(sb.ToString()).Find("tbody").Html()));
            }
        }

        #region IVisitor<IControl> Members

        public void Visit(IControl visitable)
        {
            if (context.Request.Form[visitable.Id] != null)
            {
                if (visitable.Type == ControlType.List)
                    Visit((DataGrid)visitable);
            }

            if (visitable.IsContainer)
            {
                Visit((IControlContainer)visitable);
                return;
            }
            switch (visitable.Type)
            {
                case UIT.Controls.ControlType.Button:
                    Visit((Button)visitable);
                    break;
                case UIT.Controls.ControlType.TextBox:
                    Visit((TextBox)visitable);
                    break;
                default:
                    break;
            }
        }

        #endregion
    }
}
